package com.icinfo.cloud.provider.common.aspect;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.enums.TerritoryCodeEnum;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.peripheralinterface.datareport.service.IDataReportService;
import com.icinfo.cloud.provider.punish.common.datareport.dto.CollectReportDataDto;
import com.icinfo.cloud.provider.punish.config.model.SysDepartParam;
import com.icinfo.cloud.provider.punish.config.service.ISysDepartParamService;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.HandleTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.PartiesReasonTypeEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseMainInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Map;

/**
 * @Desc 描述
 * @Author mahan
 * @Date 2023/3/29
 */
@Aspect
@Component
@Slf4j
@Order(99)
public class CaseChangeReportDataAspect {

	@Resource
	private ICaseMainInfoService caseMainInfoService;
	@Resource
	private ISysDepartParamService sysDepartParamService;
	@Resource
	private IDataReportService dataReportService;

	@Pointcut("@annotation(com.icinfo.cloud.provider.common.annotation.CaseChangeReportData)")
	public void dataReportPointCut() {

	}

	@AfterReturning(pointcut = "dataReportPointCut()", returning = "retValue")
	public void after(JoinPoint point, Object retValue) {
		Map m = (Map) retValue;
		log.info("数据上报切面参数：", JSONObject.toJSONString(m));
		//caseID
		String workId = m.get("workId") == null ? "" : m.get("workId").toString();
		//partyId
		String workSubId = m.get("workSubId") == null ? "" : m.get("workSubId").toString();
		String stepSignal = m.get("stepSignal") == null ? "" : m.get("stepSignal").toString();
		//案件流程滚动
		if (StringUtils.isNotEmpty(workId) && (StringUtils.isEmpty(stepSignal) || "STEP-GO".equals(stepSignal))) {
			CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(workId);
			//没有案件编号不上报
			if(ObjectUtil.isEmpty(caseMainInfo.getCaseNumber())){
				return;
			}
			/**
			 * 目前只支持人社条线，后续会新增其他条线
			 * 1、判断是否人社条线
			 * 2、判断是否多当事人
			 *      多当事人需要到告知阶段上报
			 */
			if (!TerritoryCodeEnum.RLSB.getCode().equals(caseMainInfo.getCaseTerritoryCode())) {
				return;
			}
			CollectReportDataDto dto = new CollectReportDataDto();
			dto.setCaseId(caseMainInfo.getId());
			dto.setCaseTerritoryCode(caseMainInfo.getCaseTerritoryCode());
			//判断是否为多当事人，多当事人处罚告知阶段以后才上报
			//多当事人
			if (PartiesReasonTypeEnum.MULTIPLE_PARTIES.getCode().equals(caseMainInfo.getCasePartiesReasonType())) {
				dto.setPartyId(workSubId);
				//处罚告知阶段才上报
				if (Arrays.asList("PUNISH_INFORM", "PUNISH_DECIDE", "PUNISH_EXECUTION", "CLOSE_ARCHIVING").contains(caseMainInfo.getCaseCurrentStageCode())) {
					if (HandleTypeEnum.PART_PUNISH.getCode().equals(caseMainInfo.getHandleType())) {
						dto.setCasePartyType("3");
					} else {
						dto.setCasePartyType("2");
					}
				}
			} else {//单当事人
				dto.setCasePartyType("1");
			}
			//调用接口
			dataReportService.caseDataReport(dto);
		} else {
			log.info("caseId为空，当前案件数据未发生滚动，无需上报数据！");
		}
	}

	/**
	 * 判断是否开启上报
	 * 简易程序上报判断在app项目中写
	 *
	 * @param caseMainInfo
	 * @return
	 */
	private boolean checkIsAllowDataReport(CaseMainInfoVo caseMainInfo) {
		String orgCode = caseMainInfo.getCaseOrgCode();
		if (StrUtil.isNotEmpty(orgCode)) {
			SysDepartParam sysDepartParam = sysDepartParamService.getSysDepartParamByOrgCodeAndDeptCode(orgCode, "");
			String isAllowDataReportStr = sysDepartParam.getIsAllowDataReport();
			if (YesOrNoEnum.YES.getCode().equals(isAllowDataReportStr)) {
				return true;
			}
		}
		return false;
	}
}
